/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1Trade;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來交易OK的封包
 */
public class C_TradeOK extends ClientBasePacket {

	private static final String C_TRADE_CANCEL = "[C] C_TradeOK";
	private static final Logger _log = Logger.getLogger(C_TradeOK.class.getName());
	@Override
	public String getType() {
		return C_TRADE_CANCEL;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			//			read(decrypt);

			final L1PcInstance player = client.getActiveChar();
			if (player == null) {
				return;
			}
			if(player.isTeleport()){
				return;
			}
			final L1Object obj = L1World.getInstance().findObject(player.getTradeID());
			if(obj == null){
				return;
			}

			L1PcInstance trading_partner = null;
			if(obj instanceof L1PcInstance){
				trading_partner =(L1PcInstance)obj;//TODO 交易對象
			}
			if (trading_partner != null) {
				if(trading_partner.isTeleport()){
					return;
				}
				player.setTradeOk(true);

				if (!player.getLocation().isInScreen(trading_partner.getLocation())
						|| player.getLocation().getTileLineDistance(trading_partner.getLocation()) > 7) {//TODO 交易的對象不在螢幕內,或者距離大於7步
					player.sendPackets(new S_SystemMessage("交易對象離你太遠。"));
					//					final L1Trade trade = new L1Trade();
					final L1Trade trade = new L1Trade();
					trade.TradeCancel(player);


				}else if (player.getTradeOk() && trading_partner.getTradeOk()){ // 共にOKを押した

					// (180 - 16)個未満ならトレード成立。
					// 本来は重なるアイテム（アデナ等）を既に持っている場合を考慮しないければいけない。
					if (player.getInventory().getSize() < 180 - 20
							&& trading_partner.getInventory().getSize() < 180 - 20){ // お互いのアイテムを相手に渡す

						//	final L1Trade trade = new L1Trade();
						final L1Trade trade = new L1Trade();
						trade.TradeOK(client ,player);

					} else { // お互いのアイテムを手元に戻す

						// \f1一個角色最多可攜帶180個道具。
						player.sendPackets(new S_ServerMessage(263));
						// \f1一個角色最多可攜帶180個道具。
						trading_partner.sendPackets(new S_ServerMessage(263));
						final L1Trade trade = new L1Trade();
						trade.TradeCancel(player);
					}

				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
